package com.lib.common.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.lib.common.R;
import com.lib.utils.XLog;

public class PopWindowHelper{
    public static final int MATCH_PARENT = ViewGroup.LayoutParams.MATCH_PARENT;
    public static final int WRAP_CONTENT = ViewGroup.LayoutParams.WRAP_CONTENT;

    private View mView;
    private int mStyle = -1;
    private TextView mTextView;
    private boolean mTouchable = true;
    private boolean mOutSideTouchable = true;
    private Drawable mBgDrawable;
    private int mX = 0;
    private int mY = 0;
    private int mWidth = WRAP_CONTENT;
    private int mHeight = WRAP_CONTENT;
    private PopupWindow mPopupWindow;

    /**
     * 此方法，会替换弹窗内部默认的控件,所以,不能使用内部控件进行赋值,防止空指针
     */
    public PopWindowHelper setView(View view){

        mView = view;
        return this;
    }

    public PopWindowHelper setBgDrawable(Drawable drawable){

        mBgDrawable = drawable;
        return this;
    }

    public PopWindowHelper setStyle(int style){

        mStyle = style;
        return this;
    }

    /**
     * Update the text in a Toast that was previously created using one of the makeText() methods.
     *
     * @param text The new text for the Toast.
     */
    public PopWindowHelper setText(String text){

        if(mTextView != null){
            mTextView.setText(text);
        }else{
            XLog.w( "setText-->自定义View了，此方法无法生效");
        }
        return this;
    }

    public PopWindowHelper setSize(int width, int height){

        mWidth = width;
        mHeight = height;
        return this;
    }

    /**
     * 是否可以触摸
     */
    public PopWindowHelper setTouchable(boolean touchable){

        mTouchable = touchable;
        return this;
    }

    /**
     * 弹窗外部是否可以触摸
     */
    public PopWindowHelper setOutsideTouchable(boolean outsideTouchable){

        mOutSideTouchable = outsideTouchable;
        return this;
    }

    public PopupWindow build(Context context){

        if(mView == null){
            mView = View.inflate(context, R.layout.view_pop_window, null);
            mTextView = mView.findViewById(R.id.pop_text);
        }
        mPopupWindow = new PopupWindow(context);
        mPopupWindow.setContentView(mView);
        mPopupWindow.setWidth(mWidth);
        mPopupWindow.setHeight(mHeight);
        mPopupWindow.setTouchable(mTouchable);
        mPopupWindow.setOutsideTouchable(mOutSideTouchable);
        mPopupWindow.setAnimationStyle(mStyle == -1 ? R.style.PopFadedAnimStyle : mStyle);
        if(mBgDrawable != null){
            mPopupWindow.setBackgroundDrawable(mBgDrawable);
        }else{
            mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        }
        return mPopupWindow;
    }
}